<template>
  <div class="login-container">
    <div class="title-container">
      <h3 class="title">失物招领后台管理系统</h3>
    </div>

    <div style="margin-left: 35%; margin-right: 35%">
      <el-tabs stretch v-model="activeName">
        <!-- 密码登录 -->
        <el-tab-pane label="密码登录" name="loginByPassword">
          <el-form ref="loginForm1" :model="loginForm1" :rules="loginRules1" class="login-form" auto-complete="on"
            label-position="left">
            <el-form-item prop="phone">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input ref="phone" v-model="loginForm1.phone" placeholder="请输入手机号" name="phone" type="text" tabindex="1"
                auto-complete="on" />
            </el-form-item>

            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input :key="passwordType" ref="password" v-model="loginForm1.password" :type="passwordType"
                placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>

            <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
              @click.native.prevent="loginByPassword">登录</el-button>
          </el-form>
        </el-tab-pane>

        <!-- 短信登录 -->
        <el-tab-pane label="短信登录" name="loginByShortMsg">
          <el-form ref="loginForm2" :model="loginForm2" :rules="loginRules2" class="login-form" auto-complete="on"
            label-position="left">
            <el-form-item prop="phone">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input ref="phone" v-model="loginForm2.phone" placeholder="请输入手机号" name="phone" type="text" tabindex="1"
                auto-complete="on" />
            </el-form-item>

            <el-form-item prop="shortMsg">

              <el-row>
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input ref="shortMsg" v-model="loginForm2.shortMsg" placeholder="请输入验证码" type="text" name="shortMsg"
                  tabindex="2" auto-complete="on" />
                <el-button size="mini" type="primary" plain @click="getShortMsg">获取验证码</el-button>
              </el-row>
            </el-form-item>
            <el-button :loading="loading" type="primary" style="width:100%; margin-bottom:30px;"
              @click.native.prevent="loginByShortMsg">登录</el-button>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { validPhone, validPassword } from '@/utils/validate'
import Cookies from 'js-cookie'
import { loginByPassword, loginByShortMsg, getShortMsg } from '@/api/auth/user'

export default {
  name: 'Login',
  data() {
    const validatePhone = (rule, value, callback) => {
      if (!validPhone(value)) {
        callback(new Error('手机号格式不正确'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!validPassword(value)) {
        callback(new Error('登录密码限制为6~18个字符，至少包含1个字母、1个数字、1个特殊字符'))
      } else {
        callback()
      }
    }
    const validateShortMsg = (rule, value, callback) => {
      if (value.length !== 6) {
        callback(new Error('验证码为 6 位'))
      } else {
        callback()
      }
    }
    return {
      loginForm1: {
        phone: '',
        password: '',
      },
      loginForm2: {
        phone: '',
        shortMsg: '',
      },
      loginRules1: {
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loginRules2: {
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        shortMsg: [{ required: true, trigger: 'blur', validator: validateShortMsg }]
      },
      activeName: 'loginByPassword',
      loading: false,
      passwordType: 'password'
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    loginByPassword() {
      this.$refs.loginForm1.validate(valid => {
        if (valid) {
          this.loading = true
          loginByPassword(this.loginForm1).then(res => {
            if (res.data.code === 200) {
              let role = res.data.roleAndPermissionDTO.roleCodeList
              Cookies.set("user_role", JSON.stringify(res.data.roleAndPermissionDTO))
              if (role === null || (role.length === 1 && role[0] === 'user')) {
                this.$message.error({
                  message: '无访问权限！',
                  duration: 1000
                })
              } else {
                Cookies.set('user', JSON.stringify(res.data.userDetailInfoVO))
                Cookies.set('user_token', JSON.stringify(res.data.tokenInfo))
                this.$router.push('/dashboard')
                this.$message.success({
                  message: res.data.message,
                  duration: 1000
                })
              }
            } else {
              // this.$message.error({
              //   message: res.data.message,
              //   duration: 1000
              // })
            }
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    loginByShortMsg() {
      let data = {
        "captcha": this.loginForm2.shortMsg,
        "phone": this.loginForm2.phone
      }
      this.$refs.loginForm2.validate(valid => {
        if (valid) {
          this.loading = true
          loginByShortMsg(data).then(res => {
            if (res.data.code === 200) {
              Cookies.set('user', JSON.stringify(res.data.userDetailInfoVO))
              Cookies.set('user_token', JSON.stringify(res.data.tokenInfo))
              this.$router.push('/dashboard')
              this.$message.success({
                message: res.data.message,
                duration: 1000
              })
            } else {
              // this.$message.error({
              //   message: res.data.errorInfo,
              //   duration: 1000
              // })
            }
          })
          this.loading = false
        } else {
          return false
        }
      })
    },
    getShortMsg() {
      let data = {
        "phone": this.loginForm2.phone
      }
      getShortMsg(data).then(res => {
        if (res.data.code === 200) {
          this.$message.success({
            message: res.data.message,
            duration: 1000
          })
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    }
  }
}
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-tabs__item {
    color: $light_gray;
  }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 70%;

    input {
      background: transparent;
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    //padding: 35px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    margin-top: 150px;
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
